﻿using UnityEngine;
using System.Collections;

public class MachineGun : RangedWeapon
{
	public int CollisionLayerMask = 1;
	private LineRenderer weaponFire;
	private int shotDistance = 100;


	// Use this for initialization
	void Start () {
		InitWeapon();

		weaponFire = GetComponent<LineRenderer>();
		weaponFire.SetVertexCount(2);
	}
	
	// Update is called once per frame
	void Update () {
		UpdateWeapon();

	}

	public override void OnAttack(Vector2 direction)
	{
		if (weaponFire)
		{
			StartCoroutine("renderTracer", direction);
		}

		RaycastHit2D hitObject = Physics2D.Raycast(this.transform.position, direction, shotDistance);
		GameObject theWeaponOwner = this.transform.parent.gameObject;


		Debug.DrawRay(this.transform.position, direction, Color.red, 0.5f);

		if (hitObject.collider && hitObject.collider.gameObject != theWeaponOwner)
		{
			Entity entityComp = hitObject.collider.gameObject.GetComponent<Entity>();
			entityComp.Hp--;
			//Destroy(hitObject.collider.gameObject);
		}


	}
	
	public override void OnReload()
	{
		
	}


	IEnumerator renderTracer(Vector2 direction)
	{
		weaponFire.enabled = true;
		weaponFire.SetPosition(0, this.transform.position);
		weaponFire.SetPosition(1, direction*shotDistance);
		yield return null;

		weaponFire.enabled = false;
	}
	
	
}
